package fr.upmc.aladyn.transactionables.saveobject;

import static org.junit.Assert.*;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import fr.upmc.aladyn.transactionables.saveobject.SaveRestoreObject;
import fr.upmc.aladyn.transactionables.tests.objects.Point;
import fr.upmc.aladyn.transactionables.tests.objects.Point3D;
import fr.upmc.aladyn.transactionables.tests.objects.Point3DWithName;


/**
 * <p>Test class</p>
 * @author Alexis Deluze
 *
 */
public class TestManuelHeritage {
	private Point3D point3d;
	private Point point;
	private Point3DWithName point3dWithName;
	private SaveRestoreObject sro;
	private SaveRestoreObject sro2;
	private SaveRestoreObject sro3;
	private int xini;
	private int yini;
	private int x2ini;
	private int y2ini;
	private int z2ini;
	private int x3ini;
	private int y3ini;
	private int z3ini;
	private String name3ini;
	
	@Before
	public void initialise(){
		System.out.println("Debut du Test TestHeritage");
		
		point   = new Point(0, 0);
		point3d = new Point3D(0, 0, 0);
		point3dWithName = new Point3DWithName(0, 0, 0, "MyPoint");
		
		xini = point.getX();
		yini = point.getY();

		x2ini = point3d.getX();
		y2ini = point3d.getY();
		z2ini = point3d.getZ();
		
		x3ini = point3dWithName.getX();
		y3ini = point3dWithName.getY();
		z3ini = point3dWithName.getZ();
		name3ini = point3dWithName.getName();
		
		sro = new SaveRestoreObject(point);
		sro2 = new SaveRestoreObject(point3d);
		sro3 = new SaveRestoreObject(point3dWithName);
	}
	
	
	@After
	public void end(){
		System.out.println("Fin du Test TestHeritage");
	}
	
	@Test
	public void test() {
		
		try {
			sro.save();
			sro2.save();
			sro3.save();
			
			point.setX(78);
			point3d.setX(2);
			point3d.setZ(9);
			
			point3dWithName.setName("changed");
			point3dWithName.setX(-80);
			point3dWithName.setZ(100);
			
			methodWithException();
			
		} catch (Exception e) {
			sro3.restore();
			sro2.restore();
			sro.restore();
		}
		
		assertTrue(xini == point.getX());
		assertTrue(yini == point.getY());
		
		assertTrue(x2ini == point3d.getX());
		assertTrue(y2ini == point3d.getY());
		assertTrue(z2ini == point3d.getZ());
		
		assertTrue(x3ini == point3dWithName.getX());
		assertTrue(y3ini == point3dWithName.getY());
		assertTrue(z3ini == point3dWithName.getZ());
		assertTrue(name3ini == point3dWithName.getName());
	}
	
	
	public void methodWithException() throws Exception{
		System.out.println("Lancement d'une exception.");
		throw new Exception("Lancement d'une exception.");
	}
}
